package com.mida.app.report.controller;


import com.mida.app.report.service.DocStructDefinitionSpaceService;
import com.mida.app.report.service.MdmService;
import com.mida.bms.report.api.DocStructDefinitionSpaceApi;
import com.mida.bms.report.entity.DocStructDefinitionSpace;
import com.mida.bms.report.vo.DocStructDefinitionSpacePageVo;
import com.mida.bms.report.vo.DocStructDefinitionSpaceVo;
import com.mida.bms.report.vo.PageResultVo;
import com.mida.dms.mdm.vo.provider.BaseOrgInfoVo;
import com.mida.dms.mdm.vo.provider.EmployeeInfoVo;
import com.mida.tool.common.cons.RespConst;
import com.mida.tool.common.exception.MassertException;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: DocStructDefinitionSpaceController
 * @Description: 结构化模板定义空间控制器
 * @Author: wang xiao le
 * @Date: 2021/6/18 15:33
 **/
@RestController
@RequestMapping("/docStructDefinitionSpace")
@CrossOrigin
@Slf4j
public class DocStructDefinitionSpaceController {

    /**
     * 结构化模板定义空间feign接口
     */
    @Autowired
    private DocStructDefinitionSpaceApi docStructDefinitionSpaceApi;


    @Autowired
    private MdmService mdmService;
    /**
     * 结构化模板定义空间Service
     */
    @Autowired
    private DocStructDefinitionSpaceService docStructDefinitionSpaceService;
    /**
     * 1、添加定义空间
     *
     * @param docStructDefinitionSpaceVo
     * @return
     */
    @PostMapping(value = "add",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ActionResult add(@RequestBody DocStructDefinitionSpaceVo docStructDefinitionSpaceVo) {
        return docStructDefinitionSpaceApi.add(docStructDefinitionSpaceVo);
    }

    /**
     * 2、更新定义空间
     *
     * @param docStructDefinitionSpaceVo
     * @return
     */
    @PostMapping(value = "update",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ActionResult update(@RequestBody DocStructDefinitionSpaceVo docStructDefinitionSpaceVo) {
        return docStructDefinitionSpaceApi.update(docStructDefinitionSpaceVo);
    }


    /**
     * 3、删除定义空间
     *
     * @param docStructDefinitionSpaceSoid 定义空间soid
     * @return null
     */
    @DeleteMapping("delete")
    @ResponseBody
    public ActionResult delete(@RequestParam long docStructDefinitionSpaceSoid) {
        return docStructDefinitionSpaceApi.delete(docStructDefinitionSpaceSoid);
    }

    /**
     * 4、获取单个定义空间
     *
     * @param docStructDefinitionSpaceSoid 定义空间soid
     * @return
     */
    @GetMapping("get")
    @ResponseBody
    public ListResult<DocStructDefinitionSpaceVo> get(
            @RequestParam(value = "docStructDefinitionSpaceSoid") long docStructDefinitionSpaceSoid) {
        try {
            return docStructDefinitionSpaceService.gainDocStructDefinitionSpaceVo(docStructDefinitionSpaceSoid);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("获取单个定义空间失败,请联系管理员"));
        }
    }

    /**
     * 5、分页查询定义空间
     *
     * @param vo
     * @return
     */
    @PostMapping(value = "findPage",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ListResult<PageResultVo<DocStructDefinitionSpaceVo>> findPage(@RequestBody DocStructDefinitionSpacePageVo vo) {
        return docStructDefinitionSpaceApi.findPage(vo);
    }

    /**
     * 6、定义空间名称重复性校验
     */
    @GetMapping(value = "/checkName")
    @ResponseBody
    public ActionResult checkName(@RequestParam(value = "name", required = true) String name,
                                  @RequestParam(value = "defineSpaceSoid", required = false) Long defineSpaceSoid) {
        return docStructDefinitionSpaceApi.checkName(name, defineSpaceSoid);
    }

    /**
     * 7、更改定义空间状态
     *
     * @param defineSpaceSoid 定义空间soid
     * @param status          状态码
     * @return
     */
    @GetMapping("/updateStatus")
    @ResponseBody
    public ActionResult update(@RequestParam(value = "status", required = true) Long status,
                               @RequestParam(value = "defineSpaceSoid", required = true) Long defineSpaceSoid) {
        return docStructDefinitionSpaceApi.updateStatus(status, defineSpaceSoid);
    }

    /**
     * 8、查询所有定义空间
     *
     * @param searchData 搜索关键字
     * @return
     */
    @GetMapping(value = "/findAll",produces = { "application/json;charset=UTF-8" })
    @ResponseBody
    public ListResult<List<DocStructDefinitionSpace>> findAll(@RequestParam(value = "searchData", required = false) String searchData) {

        ListResult result = new ListResult();
        try {
            return  docStructDefinitionSpaceService.findAll(searchData);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw new MassertException(
                    RespConst.newMsgError("查询所有授权空间失败,请联系管理员"));
        }
    }

    /**
     * 9、根据员工sooid查询医院
     *
     * @param employeeSoid 员工soid
     * @return
     */
    @GetMapping(value = "/findHospitalByEmployeeSoid", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ListResult<List<BaseOrgInfoVo>> findHospitalByEmployeeSoid(@RequestParam("employeeSoid") Long employeeSoid) {
        return mdmService.findHospitalByEmployeeSoid(employeeSoid);
    }

    /**
     * 10、模糊查询员工信息
     *
     * @param orgInfoSoid
     * @param orgInfoType
     * @param search
     * @return
     */
    @GetMapping(value = "/employee/list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ListResult<List<EmployeeInfoVo>> findListByOrgSoid(@RequestParam("orgInfoSoid") Long orgInfoSoid,
                                                              @RequestParam("orgInfoType") Long orgInfoType,
                                                              @RequestParam(value = "search", defaultValue = "") String search) {
        return mdmService.findListByOrgSoid(orgInfoSoid, orgInfoType, search);
    }

}
